package com.ruoyi.web.controller.social.examine;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.Company;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.mapper.CompanyMapper;
import com.ruoyi.system.service.CompanyService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author: ruohao
 * @Date: 2023/9/3
 **/
@RestController
@RequestMapping("/social/examine/company")
public class CompanyController extends BaseController {
    @Resource
    private CompanyService companyService;

    @Resource
    private CompanyMapper companyMapper;

    @GetMapping("/list")
    public TableDataInfo list(Company company)
    {
        startPage();
        List<Company> list = companyService.selectList(company);
        return getDataTable(list);
    }

    @GetMapping("/listAllNoOther")
    public TableDataInfo listAllNoOther()
    {
        List<Company> list = companyMapper.selectNoOther(new Company());
        return getDataTable(list);
    }

    @GetMapping("/listAll")
    public TableDataInfo listAll()
    {
        List<Company> list = companyService.selectList(new Company());
        return getDataTable(list);
    }

    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable Long id)
    {
        return success(companyService.selectById(id));
    }

    @Log(title = "添加公司", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@Validated @RequestBody Company company)
    {
        company.setCreateBy(SecurityUtils.getLoginUser().getUser().getNickName());
        Boolean result = companyService.create(company);
        if (result) {
            return success();
        }
        return error();
    }

    @Log(title = "编辑公司信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody Company company) {
        company.setUpdateBy(SecurityUtils.getLoginUser().getUser().getNickName());
        Boolean result = companyService.update(company);
        if (result) {
            return success();
        }
        return error();
    }

    @Log(title = "删除公司信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id)
    {
        return toAjax(companyService.deleteById(id));
    }
}
